﻿using DevExpress.XtraEditors;
using MesProjectWinform.Production.Models;
using MesProjectWinform.Public.Common;
using MesProjectWinform.Public.Input;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Public.Form
{
    public partial class AddDocForm : DevExpress.XtraEditors.XtraForm
    {
        public AddDocForm()
        {
            InitializeComponent();
        }

        public event EventHandler MyEvent;
        public delegate void MyDelegate(); // 定义委托类型
        public MyDelegate MyDelegateCallback;
        
        private void AddDocForm_Load(object sender, EventArgs e)
        {
            #region 初始化客户下拉框
            lookUpEdit1.Properties.DataSource = new List<TypeModel>()
            {
                new TypeModel(){Id=1,Name="编号每天从1开始增长"},
                new TypeModel(){Id=2,Name="编号每月从1开始增长"},
                new TypeModel(){Id=3,Name="编号每年从1开始增长"},
                new TypeModel(){Id=4,Name="编号累计自增"},
            };
            lookUpEdit1.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit1.Properties.ValueMember = "Id"; // 实际存储的字段

            //在显示时只显示Name字段
            lookUpEdit1.Properties.PopulateColumns();
            lookUpEdit1.Properties.Columns[0].Visible = false; // 隐藏Id字段

            //下拉框启动时默认显示请选择
            lookUpEdit1.Properties.NullText = "请选择";
            #endregion
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            DocInput input = new DocInput
            {
                Hyphen = textEdit5.Text.Trim(),
                RULETYPE = radioGroup1.Properties.Items[radioGroup1.SelectedIndex].Value.ToString(),
                RULENAME = textEdit2.Text.Trim(),
                SERVICESNAME = textEdit1.Text.Trim(),
                ADDTYPE = Convert.ToInt64(lookUpEdit1.EditValue),
                PREFIX = textEdit4.Text.Trim(),
                DOCUMENTFIEID = textEdit3.Text.Trim(),
                CODENUM = (long)spinEdit1.Value,
            };
            string data = JsonConvert.SerializeObject(input);
            var response = await HttpClientHelper.RequestUrl(RequestType.Post, MethodGolab.AddDocCode, data);
            ApiResult<int> result = JsonConvert.DeserializeObject<ApiResult<int>>(response);
            if(result.Code==ApiEnums.Success)
            {
                //MessageBox.Show($"{result.Message}");
                //MyEvent?.Invoke(this, EventArgs.Empty); // 触发事件
                //this.Close();
                MessageBox.Show($"{result.Message}");
                MyDelegateCallback?.Invoke(); // 调用委托
                this.Close();
            }
            else
            {
                MessageBox.Show($"{result.Message}");
            }
        }
    }
}